/// <reference path="../lib/gd3d.d.ts" />

//需加上这个反射标记，场景才能通过名字找到这个类，并自动创建他
@gd3d.reflect.userCode
class TestRotate implements gd3d.framework.IUserCode
{
    app: gd3d.framework.application;
    scene: gd3d.framework.scene;
    taskmgr: gd3d.framework.taskMgr = new gd3d.framework.taskMgr();
    private camera: gd3d.framework.camera;
    private cube: gd3d.framework.transform;
    private zeroPoint: gd3d.math.vector3 = new gd3d.math.vector3(0, 0, 0);
    private light: gd3d.framework.light;
    onStart(app: gd3d.framework.application)
    {
        let that = this;
        this.app = app;
        this.scene = this.app.getScene();
        //添加一个摄像机
        var objCam = new gd3d.framework.transform();
        objCam.name = "camera";
        this.scene.addChild(objCam);
        this.camera = objCam.gameObject.addComponent(gd3d.framework.StringUtil.COMPONENT_CAMERA) as gd3d.framework.camera;
        this.camera.near = 0.01;
        this.camera.far = 1000;
        this.camera.fov = 45;
        objCam.localTranslate = new gd3d.math.vector3(0, 3, -3);
        objCam.lookatPoint(this.zeroPoint);
        objCam.markDirty();//标记为需要刷新

        // let cb = new gd3d.framework.transform();
        // cb.name = "cube";
        // cb.localScale.x = cb.localScale.y = cb.localScale.z = 0.5;
        // cb.localTranslate = new gd3d.math.vector3(0, 0, 0);
        // // this.scene.addChild(cb);
        // var mesh = cb.gameObject.addComponent(gd3d.framework.StringUtil.COMPONENT_MESHFILTER) as gd3d.framework.meshFilter;
        // var smesh = this.app.getAssetMgr().getDefaultMesh("cube");
        // mesh.mesh = (smesh);
        // cb.gameObject.addComponent(gd3d.framework.StringUtil.COMPONENT_MESHRENDER);
        // cb.markDirty();

        var lighttran = new gd3d.framework.transform();
        this.scene.addChild(lighttran);
        this.light = lighttran.gameObject.addComponent(gd3d.framework.StringUtil.COMPONENT_LIGHT) as gd3d.framework.light;
        this.light.type = gd3d.framework.LightTypeEnum.Direction;
        // lighttran.addChild(cb);
        lighttran.localTranslate = new gd3d.math.vector3(-1, 1, -1);
        lighttran.localEulerAngles = new gd3d.math.vector3(45, 45, 0);

        lighttran.markDirty();
        // this.addDomUI();

        this.taskmgr.addTaskCall(this.loadShader.bind(this));
        this.taskmgr.addTaskCall(this.loadText.bind(this));
        this.taskmgr.addTaskCall(this.loadMesh.bind(this));
    }

    private loadShader(laststate: gd3d.framework.taskstate, state: gd3d.framework.taskstate)
    {
        this.app.getAssetMgr().loadCompressBundle("resources/shader/shader.assetbundle.json", (_state) =>
        {
            if (_state.isfinish)
            {
                state.finish = true;
            }
        }
        );
    }

    private loadMesh(laststate: gd3d.framework.taskstate, state: gd3d.framework.taskstate)
    {
        let meshName: string = "45678";
        this.app.getAssetMgr().load("resources/prefabs/" + meshName + "/" + meshName + ".assetbundle.json", gd3d.framework.AssetTypeEnum.Auto, (_state) =>
        {
            if (_state.isfinish)
            {
                state.finish = true;
                let _prefab2: gd3d.framework.prefab = this.app.getAssetMgr().getAssetByName(meshName + ".prefab.json") as gd3d.framework.prefab;
                this.cube = _prefab2.getCloneTrans();
                this.scene.addChild(this.cube);
                this.cube.localTranslate = new gd3d.math.vector3(0, 0, 0);
                this.cube.localScale = new gd3d.math.vector3(1, 1, 1);
                
                // var sh = this.app.getAssetMgr().getShader("light1.shader.json");
                var sh = this.app.getAssetMgr().getShader("normalmap.shader.json");
                if (sh != null)
                {
                    let meshRender = this.cube.gameObject.getComponent(gd3d.framework.StringUtil.COMPONENT_MESHRENDER) as gd3d.framework.meshRenderer;
                    let filter = this.cube.gameObject.getComponent(gd3d.framework.StringUtil.COMPONENT_MESHFILTER) as gd3d.framework.meshFilter;
                    // filter.mesh = this.app.getAssetMgr().getDefaultMesh("cube");
                    // this.cube.localScale = new gd3d.math.vector3(1, 1, 1);
                    meshRender.materials = [];
                    meshRender.materials.push(new gd3d.framework.material());
                    meshRender.materials[0].setShader(sh);
                    let texture = this.app.getAssetMgr().getAssetByName("LV.png") as gd3d.framework.texture;
                    meshRender.materials[0].setTexture("_MainTex", texture);

                    let normalTexture = this.app.getAssetMgr().getAssetByName("normal.png") as gd3d.framework.texture;
                    meshRender.materials[0].setTexture("_NormalTex", normalTexture);
                }
                this.cube.markDirty();
            }
        }
        );
    }

    private loadText(laststate: gd3d.framework.taskstate, state: gd3d.framework.taskstate)
    {
        let c = 0;
        c++;
        this.app.getAssetMgr().load("resources/LV.png", gd3d.framework.AssetTypeEnum.Auto, (s) =>
        {
            if (s.isfinish)
            {
                c--;
                if (c == 0)
                    state.finish = true;
            }
            else
            {
                state.error = true;
            }
        }
        );
        c++;
        this.app.getAssetMgr().load("resources/normal.png", gd3d.framework.AssetTypeEnum.Auto, (s) =>
        {
            if (s.isfinish)
            {
                c--;
                if (c == 0)
                    state.finish = true;
            }
            else
            {
                state.error = true;
            }
        }
        );

    }

    // private addDomUI()
    // {
    //     var btn = document.createElement("button");
    //     btn.textContent = "切换光源类型";
    //     btn.onclick = () =>
    //     {
    //         if (this.light != null)
    //         {
    //             if (this.light.type == gd3d.framework.LightTypeEnum.Direction)
    //             {
    //                 this.light.type = gd3d.framework.LightTypeEnum.Point;
    //                 console.log("点光源");
    //             }
    //             else if (this.light.type == gd3d.framework.LightTypeEnum.Point)
    //             {
    //                 this.light.type = gd3d.framework.LightTypeEnum.Direction;
    //                 // this.light.spotAngelCos = Math.cos(0.2 * Math.PI);
    //                 console.log("方向光");
    //             } 
    //         }
    //     }
    //     btn.style.top = "124px";
    //     btn.style.position = "absolute";
    //     this.app.container.appendChild(btn);
    // }

    private timer: number = 0;
    onUpdate(delta: number)
    {
        this.taskmgr.move(delta);
        this.timer += delta;
        if (this.cube != null)
        {
            // this.cube.localTranslate.x = Math.cos(this.timer) * 1.0;
            // this.cube.localTranslate.z = Math.sin(this.timer) * 1.0;
            // this.cube.localEulerAngles.y = Math.cos(this.timer) * 1.0;
            // this.cube.localEulerAngles.y = this.timer * 10.0;
            this.cube.localEulerAngles = new gd3d.math.vector3(-90, this.timer * 40.0, 0);
            // this.cube.lookatPoint(this.zeroPoint);
            this.cube.markDirty();
        }
    }

    isClosed(): boolean
    {
        return false;
    }
}
